<template>
  <div>
    <!-- 渲染角色列表 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="obj in roleList"
        :key="obj.id"
        :label="obj.id"
      >{{ obj.name }}</el-checkbox>

    </el-checkbox-group>

    <el-row class="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="submit">确定</el-button>
        <el-button size="small" @click="cancleDialog">取消</el-button>
      </el-col>
    </el-row>
  </div>
</template>

<script>
// 引入角色列表接口
import { getRole } from '@/api/department'
// 导入获取员工详情的 API
import { getUserDetailById } from '@/api/user'
// 分配角色
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    pessionId: {
      type: [String, Number],
      required: true
    }
  },
  data() {
    return {
      roleIds: [], // 保存已有的权限列表
      roleList: []// 保存权限列表数据
    }
  },
  created() {
    // 调用角色列表数据方法
    this.getRoleFn()
    // 获取员工已有角色方法
    this.getUserDetailByIdFn()
  },
  methods: {
    // 点击确定，调用接口，完成角色分配
    async  submit() {
      const res = await assignRoles({ id: this.pessionId, roleIds: this.roleIds })
      //   console.log(res)
      this.$message.success(res.message)// 成功提示
      this.$emit('close')
    },
    // 获取员工已有角色
    async getUserDetailByIdFn() {
      const res = await getUserDetailById(this.pessionId)
      //   console.log(res)
      this.roleIds = res.data.roleIds// 赋值给角色已有的权限
    },
    // 获取角色列表数据
    async getRoleFn() {
      const res = await getRole()
      //   console.log(res)
      this.roleList = res.data.rows
    },
    // 取消
    cancleDialog() {
      // 通知父亲关闭弹框
      this.$emit('close')
    }
  }
}
</script>

<style>
.footer {
  margin-top: 30px;
}
</style>
